﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;
namespace WebApplication.Admin
{
    public partial class BoPhanAdd : System.Web.UI.Page
    {
        IUnitOfWork iunitfwork = new EFUnitOfWork();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadHienThi();
            }
        }
        void LoadHienThi()
        {
            string strid = "" + Request["MABP"];
            BOPHANRepository repository = new BOPHANRepository(new EFRepository<BOPHAN>(), iunitfwork);
            BOPHAN objBoPhan = null;
            if (!string.IsNullOrEmpty(strid))
            {
                objBoPhan = repository.GetById(strid);
                txtmabp.Value = objBoPhan.MABP;
                txttebbp.Value = objBoPhan.TENBOPHAN;
                if (objBoPhan.TINHTRANG == true)
                {
                    rdhd.Checked = true;
                }
                else
                {
                    rdkohd.Checked = false;
                }
            }
        }
        public void Capnhap()
        {
            bool iscapnhap = true;
            string strid = "" + Request["MABP"];
            BOPHANRepository repository = new BOPHANRepository(new EFRepository<BOPHAN>(), iunitfwork);
            BOPHAN objBoPhan = null;
            if (!string.IsNullOrEmpty(strid))
            {
                objBoPhan = repository.GetById(strid);
                iscapnhap = false;
            }
            else
            {
                objBoPhan = new BOPHAN();
                iscapnhap = true;
            }
            objBoPhan.MABP = txtmabp.Value;
            objBoPhan.TENBOPHAN = txttebbp.Value;
            if (rdhd.Checked)
            {
                objBoPhan.TINHTRANG = true;
            }
            else
            {
                objBoPhan.TINHTRANG = false;
            }
            try
            {
                if (iscapnhap)
                {
                    repository.Add(objBoPhan);
                }
                repository.Save();
            }
            catch (Exception ex)
            {
                Response.Write("<Script> alert('Lỗi rồi')</script>");
            }
        }

        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            Capnhap();
        }

        protected void lbtnthemmoi_Click(object sender, EventArgs e)
        {
            txtmabp.Value = "";
            txttebbp.Value = "";
            Response.Redirect("BoPhanAdd.aspx");
        }
    }
}